package test02;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Scanner;

public class StudentGradeSystem {
    private ArrayList<StudentScore> StudentList = new ArrayList<>();
//	选择系统的菜单
	public void Menu() {
		System.out.println("\t\t*****************************");
		System.out.println("\t\t*   1.显示所有学生的成绩记录       *");
		System.out.println("\t\t*   2.添加学生的课程成绩          *");
		System.out.println("\t\t*   3.更新学生的成绩记录          *");
		System.out.println("\t\t*   4.删除学生的课程成绩记录       *");
		System.out.println("\t\t*   5.统计平均分、最高分、最低分     *");
		System.out.println("\t\t*   6.学号查询学生的成绩记录       *");
		System.out.println("\t\t*   7.排序由高到低的成绩记录       *");
		System.out.println("\t\t*   8.退出学生成绩系统           *");
		System.out.println("\t\t*****************************");
	}
//	显示所有学生的成绩记录
	public void viewStudentScore() {
		for(StudentScore student : StudentList) {
			//输入学生成绩信息记录
			System.out.println("序号\t姓名\t学号\t\t课程名称\t\t成绩\t");
			System.out.println(student.getId()+"\t"+student.getName()+"\t"
			+student.getSno()+"\t"+student.getCname()+"\t"+student.getScore());
		}
	}
    public void addStudentScore() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生姓名: ");
        String name = scanner.nextLine();
        System.out.println("请输入学生学号: ");
        Long sno = Long.parseLong(scanner.nextLine());
        System.out.println("请输入课程名: ");
        String courseName = scanner.nextLine();
        System.out.println("请输入分数: ");
        int score = Integer.parseInt(scanner.nextLine());
        int Sid = StudentList.size() + 1; // 设置新学生的ID
        StudentScore student = new StudentScore(Sid,name,sno,courseName,score); // 创建新学生对象
        StudentList.add(student);// 将新学生对象添加到学生列表中
    }

    public void AddStudentScore(StudentScore student) {
        StudentList.add(student);
        System.out.println("学生成绩已添加");
    }

    public void UpdateStudentScore() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改成绩的学生学号: ");
        Long sno = Long.parseLong(scanner.nextLine());
        
        boolean found = false;
        ListIterator<StudentScore> iterator = StudentList.listIterator();
        while (iterator.hasNext()) {
            StudentScore student = iterator.next();
            if (student.getSno().equals(sno)) {
                System.out.println("请输入课程名: ");
                String courseName = scanner.nextLine();
                if (student.getCname().equals(courseName)) {
                    System.out.println("请输入新的成绩: ");
                    int newScore = Integer.parseInt(scanner.nextLine());
                    student.setScore(newScore);
                    StudentList.set(iterator.previousIndex(), student);
                    System.out.println("成绩已更新");
                    found = true;
                    break;
                }
            }
        }
        
        if (!found) {
            System.out.println("未找到该学生或该学生未参加该课程");
        }
    }

    public void deleteStudentScore() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除成绩的学生学号: ");
        Long sno = Long.parseLong(scanner.nextLine());
        
        System.out.println("请输入课程名: ");
        String courseName = scanner.nextLine();
        
        Iterator<StudentScore> iterator = StudentList.iterator();
        boolean found = false;
        while (iterator.hasNext()) {
            StudentScore student = iterator.next();
            if (student.getSno().equals(sno) && student.getCname().equals(courseName)) {
                iterator.remove();
                System.out.println("成绩已删除");
                found = true;
                break;
            }
        }
        
        if (!found) {
            System.out.println("未找到该学生或该学生未参加该课程");
        }
    }

    public void calculateCourseStats() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入课程名: ");
        String courseName = scanner.nextLine();
        
        int sum = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int count = 0;
        
        for (StudentScore student : StudentList) {
            if (student.getCname().equals(courseName)) {
                sum += student.getScore();
                max = Math.max(max, student.getScore());
                min = Math.min(min, student.getScore());
                count++;
            }
        }
        
        if (count > 0) {
            System.out.println("平均分: " + (double) sum / count);
            System.out.println("最高分: " + max);
            System.out.println("最低分: " + min);
        } else {
            System.out.println("未找到该课程的学生");
        }
    }

    public void findSGradeBySno() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生ID: ");
        Long sno = Long.parseLong(scanner.nextLine());
        for (StudentScore student : StudentList) {
        	if (student.getSno().equals(sno)) {
            	System.out.println("序号\t姓名\t学号\t\t课程名称\t\t成绩\t");
                System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getSno() +
                        "\t " + student.getCname() + "\t " + student.getScore()+"\t");
                return;
            }
        }
        System.out.println("未找到该学生");
    }

    public void sortStudentsByScore() {
    	ArrayList<StudentScore> sortedList = new ArrayList<>(StudentList);
        sortedList.sort(Comparator.comparingInt(StudentScore::getScore).reversed());
        for (StudentScore student : sortedList) {
            System.out.println("序号\t姓名\t学号\t\t课程名称\t\t成绩\t");
            System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getSno() +
                    "\t " + student.getCname() + "\t " + student.getScore()+"\t");
        }
    }
}
